package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.dto.CollectionBehaviorDto;
import com.heima.article.entity.ApCollection;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.IApCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.Threadlocals.UserThreadLocalUtil;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP收藏信息表 服务实现类
 * </p>
 *
 * @author 王苑
 * @since 2022-08-04
 */
@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements IApCollectionService {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Value("${topic.collectionTopic}")
    private String collectionTopic;

    /**
     * 根据实体ID和文章ID查询是否收藏
     *
     * @param articleid
     * @param entryid
     * @return
     */
    @Override
    public ApCollection findByArticleidAndEntyrid(Integer articleid, Integer entryid) {
        LambdaQueryWrapper<ApCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApCollection::getEntryId, entryid);
        wrapper.eq(ApCollection::getArticleId, articleid);
        ApCollection one = this.getOne(wrapper);

        return one;
    }

    /**
     * 收藏文章 实现类
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectionArticle(CollectionBehaviorDto dto) {
        //在线程中获取用户
        User user = UserThreadLocalUtil.get();

        //2.构建收藏文章实体对象 封装参数；进行保存
        //2.1 先查询是否已经收藏了该文章
        LambdaQueryWrapper<ApCollection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApCollection::getEntryId,user.getUserId());
        queryWrapper.eq(ApCollection::getArticleId,dto.getArticleId());
        ApCollection one = this.getOne(queryWrapper);
        if (one != null){
            //已经收藏了该文章,判断操作类型
            if (dto.getOperation().equals(1)){ //为1 是取消收藏
                this.remove(queryWrapper);
            }
        }else {
            //2.2如果没有收藏记录 ，则新增收藏
            ApCollection apCollection = new ApCollection();
            apCollection.setEntryId(user.getUserId());
            apCollection.setArticleId(dto.getArticleId());
            apCollection.setCollectionTime(new Date());
            this.save(apCollection);
        }

        //3.发送消息到Kafka
        //3.1 构建发送关注行为实体类
        CollectionBehaviorDto collectionBehaviorDto = new CollectionBehaviorDto();
        collectionBehaviorDto.setArticleId(dto.getArticleId());
        collectionBehaviorDto.setEntryId(Long.valueOf(user.getUserId()));
        collectionBehaviorDto.setEquipmentId(dto.getEquipmentId());
        collectionBehaviorDto.setOperation(dto.getOperation());
        collectionBehaviorDto.setType(dto.getType());
        //发送消息到kafka
        kafkaTemplate.send(collectionTopic, JSON.toJSONString(collectionBehaviorDto));


        return ResponseResult.okResult();
    }
}
